<?php
class Link
{
	public static function Build($link)
	{
		$base = 'http://' . getenv('SERVER_NAME');

		// If HTTP_SERVER_PORT is defined and different than default
		if (defined('HTTP_SERVER_PORT') && HTTP_SERVER_PORT != '80')
		{
			// Append server port
			$base .= ':' . HTTP_SERVER_PORT;
		}

		$link = $base . VIRTUAL_LOCATION . $link;
		// Escape html
		return htmlspecialchars($link, ENT_QUOTES);
	}
	
	public static function ToMenu($menuId)
	{
		$link = 'index.php?MenuId=' . $menuId;
		
		return self::Build($link);
	}
	
	public static function ToGenre($genreId, $page = 1)
	{
		$link = 'index.php?MenuId=3&GenreId=' . $genreId;
		
		if ($page > 1)
			$link .= '&Page=' . $page;
		
		return self::Build($link);
	}
	
	public static function ToCategory($categoryId, $page = 1)
	{
		$link = 'index.php?MenuId=3&CategoryId=' . $categoryId;
		
		if ($page > 1)
			$link .= '&Page=' . $page;
			
		return self::Build($link);
	}
	
	public static function ToMerchandise($merchandiseId)
	{
		
		$link = 'index.php?MenuId=3&MerchandiseId=' . $merchandiseId;
		
		return self::Build($link);
	}
    
    public static function QueryStringToArray($queryString)
    {
        $query = array();
        
        if ($queryString != '')
        {
            $elements = explode('&',$queryString);
            
            foreach($elements as $key=>$value)
            {
                $element = explode('=', $value);
                $result[urldecode($element[0])] = isset($element[1]) ? urldecode($element[1]) : '';
            }
        }
        
        return $result;
    }
}
?>